<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;

class SupplierController extends Controller
{
    use HasResourceActions;

    //用户首页
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header('供应商列表');
            $content->body($this->grid());
        });
    }

    //编辑
    public function edit($id, Content $content)
    {
        return $content->header('供应商信息')->description('编辑')->body($this->form()->edit($id));
    }

    //编辑保存
    public function update($id)
    {
        return $this->form()->update($id);
    }

    //查看用户详情
    public function show($id, Content $content)
    {
        return $content
            ->header('供应商信息')
            ->description('详情')
            ->body($this->detail($id));
    }

    //获取用户
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->column('id', 'ID')->sortable();
            $grid->column('name', '名称');
            $grid->column('phone', '注册手机号');
            $grid->column('id_card', '身份证号');
            $grid->column('identity', '身份')->display(function ($identity) {
                if($this->is_partner == 2) {
                    if($identity == 1) {
                        return '供应商加盟';
                    }elseif($identity == 2) {
                        return '品牌店加盟';
                    }else{
                        return '代理商加盟';
                    }
                }elseif($this->is_partner == 1) {
                    if($identity == 1) {
                        return '供应商加盟(待审核)';
                    }elseif($identity == 2) {
                        return '品牌店加盟(待审核)';
                    }else{
                        return '代理商加盟(待审核)';
                    }
                }else{
                    return '';
                }
            });
            $grid->column('alipay_account', '支付宝收款账号');
            $grid->column('alipay_name', '支付宝收款姓名');
            $grid->column('bank_name', '银行账号名');
            $grid->column('bank_number', '银行卡号');
            $grid->column('bank_type', '银行卡类型');
            $grid->column('opening_bank', '开户行');
            $grid->column('serial_number', '商家编号');
            $grid->column('partner_address', '地址')->style('max-width:200px;word-break:break-all;');
            $grid->column('issue_orders', '发布订单数');
            $grid->column('created_at', '注册时间');

            $grid->disableCreateButton();

            $grid->disableRowSelector();

            $grid->model()->whereIn('identity', ['1', '2', '3']);

            $grid->actions(function ($actions) {
                $actions->disableDelete();
            });

            $grid->tools(function ($tools) {
                // 禁用批量删除按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
                $filter->column(1/3, function ($filter) {
                    $filter->like('name', '用户名');
                });
                $filter->column(1/3, function ($filter) {
                    $filter->like('phone', '注册手机');
                });
                $filter->column(1/3, function ($filter) {
                    $filter->like('serial_number', '商家编号');
                });
            });

            $grid->model()->orderBy('id', 'desc');

            //禁用导出按钮
            $grid->disableExport();
        });
    }

    //修改表单事项
    protected function form()
    {
        // 创建一个表单
        return Admin::form(User::class, function (Form $form) {
            $form->text('name', '名称');
            $form->text('phone', '注册手机号');
            $form->text('id_card', '身份证号');
            $form->image('id_card_positive', '身份证正面')->rules('image');
            $form->image('id_card_contrary', '身份证反面')->rules('image');
            $form->select('identity', '身份')->options([1 => '供应商加盟', 2 => '品牌店加盟', 3 => '代理商加盟']);
            $form->text('alipay_account', '支付宝收款账号');
            $form->text('alipay_name', '支付宝收款姓名');
            $form->text('bank_name', '银行账号名');
            $form->text('bank_number', '银行卡号');
            $form->text('bank_type', '银行卡类型');
            $form->text('opening_bank', '开户行');
            $form->text('serial_number', '商家编号');
            $form->text('issue_orders', '发布订单数');
            $form->text('partner_name', '通知姓名');
            $form->text('partner_phone', '通知手机号');
            $form->text('partner_address', '通知详细地址');
            $form->text('partner_phone', '通知手机号');
//            $form->select('is_partner', '商家审核是否通过')->options([0 => '未提交', 1 => '审核中', 2 => '成功']);

            // 定义事件回调，当模型即将保存时会触发这个回调
            $form->saving(function (Form $form) {
                $serial_number = DB::table('users')->where('serial_number', $form->serial_number)
                    ->where('id', '<>',$form->model()->id)->first();
                if(!empty($serial_number)) {
                    $error = new MessageBag([
                        'title'   => '商家编号重复',
                        'message' => '商家编号已存在，请校验后输入',
                    ]);

                    return back()->with(compact('error'));
                }
            });
            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
            });
        });
    }

    /**
     * 用户详情
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));
        $show->id('ID');
        $show->name('名称');
        $show->phone('注册手机号');
        $show->id_card('身份证号');
        $show->id_card_positive('身份证正面')->image();
        $show->id_card_contrary('身份证反面')->image();
        $show->identity('身份')->using([1 => '供应商加盟', 2 => '品牌店加盟', 3 => '代理商加盟']);
        $show->alipay_account('支付宝收款账号');
        $show->alipay_name('支付宝收款姓名');
        $show->bank_name('银行账号名');
        $show->bank_number('银行卡号');
        $show->bank_type('银行卡类型');
        $show->opening_bank('开户行');
        $show->serial_number('商家编号');
        $show->openid('openid');
        $show->issue_orders('发布订单数');
        $show->partner_name('通知姓名');
        $show->partner_phone('通知手机号');
        $show->partner_address('通知详细地址');
        $show->is_partner('是否通过')->using([0 => '未提交', 1 => '审核中', 2 => '成功']);
        $show->created_at('注册时间');

        $show->panel()->tools(function ($tools) {
            $tools->disableDelete();
            $tools->disableEdit();
        });

        return $show;
    }
}
